//var searchUrlList={
//		"searchAllUrl":"",
//		"addUrl":"",
//		"changeStatusUrl":"",
//		"editUrl":"",
//		"deleteUrl":""
//}
var searchAllUrl = "/super/common/examine/searchSchoolDynamics";
var addUrl = "../informationMana/addNews.html";
var changeStatusUrl = "/super/common/examine/changeStatus";
var editUrl = "../informationMana/addNews.html";
var searchurl = {
	searchAllUrl : "/super/common/examine/searchSchoolDynamics",
	addUrl : "../informationMana/addNews.html",
	changeStatusUrl : "/super/common/examine/searchSchoolDynamics/changeStatus",
	editUrl : "../informationMana/addNews.html"
}


var searchurl1 = {
		searchAllUrl : "/super/common/examine/listMarx",
		addUrl : "javascript:;",
		editUrl : "javascript:;",
		"addUrl.addHtml" :"data-toggle='modal' data-target='#addMayuan'",
		deleteUrl:"/super/common/examine/searchSchoolDynamics/deletMarx"
	}
// 表头对象
var titleObj = {
	"title" : "标题",
	"brief" : "摘要",
	"creatTime" : "创建时间",
	"status" : "状态",
	"other.reduce" : "操作",
	"style.th" : { // th 样式设定
		"status" : "texC",
		"other.reduce" : "text-center"
	},
	"style.td" : { // td 样式设定
		"status" : "texC",
		"other.reduce" : "texC colorBlue"
	},
	"style.html" : { // 自定义内容，如果有逻辑代码的就加入这个
		"status" : "$1 | <a href='$url'><span class='colorBlue cursorP'>$2</span></a>",
		"other.reduce" : "<a href='$editUrl'><span class='texC  cursorP' data-toggle='modal' data-target='#editYanjiu'>编辑</span></a> "
	},
	"style.method" : { // 自定义方法，要求返回html
		"status" : "readyStatus",
		"other.reduce" : "readyReduce"
	}
}
var titleObj1 = {
	"marxSchool" : "马院名称",
	"url" : "链接",
	"creatTime" : "创建时间",
	"other.reduce" : "操作",
	"style.th" : { // th 样式设定
		"status" : "texC",
		"other.reduce" : "text-center"
	},
	"style.td" : { // td 样式设定
		"status" : "texC",
		"other.reduce" : "texC colorBlue"
	},
	"style.html" : { // 自定义内容，如果有逻辑代码的就加入这个
//		"status" : "$1 | <a href='$url'><span class='colorBlue cursorP'>$2</span></a>",
		"other.reduce" : "<span class='texC  cursorP' data-toggle='modal' data-target='#editMayuan'>编辑</span> | <a href='javascript:;' onClick='deleteByUrl(\"$1\",\"$2\")'> <span class='cursorP'>删除</span></a>"
	},
	"style.method" : { // 自定义方法，要求返回html
//		"status" : "readyStatus1",
		"other.reduce" : "readyReduce1"
	}
}

function ajaxData(type, status, page, size, title) {
	this.type = type;
	this.status = status;
	this.page = page;
	this.size = size;
	this.title = title;
}
ajaxData.prototype = {
	constructor : ajaxData,
	pageadd : function() {
		// alert(this.page);
		this.page++;
	}
}

// 请求数据格式
// var ajaxData = {
// "type" : "",
// "status" : [],
// "page" : 0,
// "size" : 10
// }
var pageName = {
	"1" : {
		"name" : "专题研究",
		"ajaxData" : new ajaxData("RESEARCH", [], 0, 10),
		"tableData" : titleObj,
		"searchUrl":searchurl
	// "addmethod":"append"
	},
	"2" : {
		"name" : "专家观点",
		"ajaxData" : new ajaxData("OPINION", [], 0, 10),
		"tableData" : titleObj,
		"searchUrl":searchurl
	// "addmethod":"append"
	},
	"3" : {
		"name" : "学校动态",
		"ajaxData" : new ajaxData("DYNAMICS", [], 0, 10),
		"tableData" : titleObj,
		"searchUrl":searchurl
	// "addmethod":"append"
	},
	"4" : {
		"name" : "会议报告",
		"ajaxData" : new ajaxData("REPORT", [], 0, 10),
		"tableData" : titleObj,
		"searchUrl":searchurl
	// "addmethod":"append"
	},
	"5" : {
		"name" : "学校成果",
		"ajaxData" : new ajaxData("HARVEST", [], 0, 10),
		"tableData" : titleObj,
		"searchUrl":searchurl
	// "addmethod":"append"
	},
	"6" : {
		"name" : "重点马院",
		"ajaxData" : new ajaxData("HARVEST", [], 0, 20),
		"tableData" : titleObj1,
		"searchUrl":searchurl1
	// "addmethod":"append"
	}
}
$(function() {
	for (i in pageName) {
		ajaxReadyTable1("GET", pageName[i]["ajaxData"], pageName[i]["searchUrl"]["searchAllUrl"],
				getTable1, pageName[i]["tableData"], i);
	}
});

// ajax数据判断
function ajaxReadyTable1(method, ajaxData, url, returnMethod, otherObj, id) {
	ajaxData = juge(ajaxData);
	var extrobj = new Array();
	extrobj.push(otherObj);
	extrobj.push(id);
	AjaxSubmit(method, ajaxData, url, returnMethod, extrobj);
}

function getTable1(obj, extrobj) {
	personData(obj, extrobj[0], extrobj[1]);
	// extrobj[0].pageadd();
}
function addTable(obj, extrobj) {
	var htmls = dealReturnData(obj, extrobj[0]);
	$("#table" + extrobj[1]).html(htmls);
}
/**
 * 组装整个代码
 * 
 * @returns
 */
function personData(obj, extrobj, id) {
	if (typeof (typeMethod) == "undefined") {
		typeMethod = "append";
	}
	var htmls = "<div class='gong-tablebigDiv bgWhite'>";
	htmls += "<div class='gong-tableT'>" + pageName[id]["name"];

	htmls += "<div class='form-group fr'>";
	htmls += "<input type='text' class='form-control input" + id
			+ "' placeholder='请输入搜索内容' id='input" + id + "' >";
	htmls += "<img src='../img/table-search.png' class='searchImg'></div></div>";
	htmls += reduceAddButton("http://www.baidu.com",extrobj);
	htmls += "<div class='gong-tableDiv fontyahei' id='table" + id + "'>";
	htmls += dealReturnData(obj, extrobj);
	htmls += "</div>"
	$('#body').append(htmls);
	// 监听事件
	$(".input" + id)
			.keyup(
					function() {
						if ($(this).val().trim() === ''
								|| $(this).val().trim() == null) {
							return;
						}
						pageName[id]["ajaxData"].title = $(this).val();
						ajaxReadyTable1("GET", pageName[id]["ajaxData"],
								searchAllUrl, addTable, titleObj, id);
					})
}

/**
 * 需要添加自己的逻辑代码 状态栏
 * 
 * @param obj
 * @param i
 * @param titleobj
 * @returns
 */
function readyStatus(obj, i, titleobj) {
	var statusHtml = titleobj["style.html"][i];
	var status = obj[i];
	status1 = status == "AUDITED" ? "已发布" : "未发布";
	status2 = status == "AUDITED" ? "关闭" : "未发布";
	statusHtml = statusHtml.replace("$1", status1);
	statusHtml = statusHtml.replace("$2", status2);
	statusHtml = statusHtml.replace("$url", changeStatusUrl + "?id=" + obj.id);
	return statusHtml;
}

/**
 * 自己的逻辑代码
 * 
 * @param obj
 * @param i
 * @param titleobj
 * @returns
 */
function readyReduce(obj, i, titleobj) {
	var statusHtml = titleobj["style.html"][i];
	statusHtml = statusHtml.replace("$editUrl", editUrl + "?nid=" + obj.id
			+ "&needUpdate=1");
	// statusHtml = statusHtml.replace("$deleteUrl", deleteUrl + "?nid=" +
	// obj.id);
	return statusHtml;
}
function readyReduce1(obj, i, titleobj){
	var statusHtml = titleobj["style.html"][i];
	statusHtml = statusHtml.replace("$1", pageName["6"]["searchUrl"]["deleteUrl"]);
	statusHtml = statusHtml.replace("$2", obj.id);
	return statusHtml;
}
/**
 * 分装整个表格
 * 
 * @param obj
 * @param extrobj
 * @returns
 */
function dealReturnData(obj, extrobj) {
	var htmls = "<table class='table table-bordered'>"
	htmls += reduceTitle(extrobj);
	htmls += "<tbody class='" + pageUndefined(extrobj["style.tbody"], "")
			+ "'>";
	var data = obj.obj.content;
	for (var i = 0; i < data.length; i++) {
		htmls += reduceBodyTable(data[i], extrobj);
	}
	htmls += "</tbody>";
	htmls += "</table>"
	return htmls;
}
/**
 * 分装新增按钮
 * 
 * @param url
 * @returns
 */
function reduceAddButton(url,extrobj) {
	var htmls = "<div class='gong-tableBarDiv'>";
	htmls += "<a type='button' href='"
			+ url
			+ "'class='texC bar-a'"+extrobj[""]+"> <span class='glyphicon glyphicon-plus'></span><span>新增</span></a></div>";
	return htmls;
}

// 分装title方法
function reduceTitle(obj) {
	var htmls = "<thead><tr>";
	for (i in obj) {
		if (i.indexOf("style.") >= 0) {
			continue;
		}
		htmls += "<th class='" + pageUndefined(obj["style.th"][i], "") + "'>"
				+ obj[i] + "</th>"
	}
	htmls += "</tr></thead>";
	return htmls;
}

// 封装tr
function reduceBodyTable(obj, titleobj) {
	var htmls = "<tr>";
	for (i in titleobj) {
		if (i.indexOf("style.") >= 0) {
			continue;
		}
		if (typeof (titleobj["style.method"][i]) == "undefined"
				&& typeof (titleobj["style.html"][i]) == "undefined") {
			htmls += "<td class='" + pageUndefined(titleobj["style.td"][i], "")
					+ "'>" + eval('obj.' + i) + "</td>";
			continue;
		}
		var funName = htmls += "<td class='"
				+ pageUndefined(titleobj["style.td"][i], "") + "'>"
				+ eval(titleobj["style.method"][i] + "(obj, i, titleobj)")
				+ "</td>";
	}
	htmls += "</tr>";
	return htmls;
}

/**
 * 判空
 * 
 * @param obj
 * @param defaultData
 * @returns
 */
function pageUndefined(obj, defaultData) {
	if (typeof (obj) == "undefined") {
		return defaultData;
	}
	return obj;
}

function deleteByUrl(url,id){
	AjaxSubmit("POST", {"id":id}, url,deleteReturnFun);
}
function deleteReturnFun(res){
	alert("操作成功！");
}
function juge(obj) {
	var result = {};
	for (i in obj) {
		if (obj[i] == null || obj[i] == "") {
			continue;
		}
		var fieldOrFun = eval("obj." + i);
		if (typeof (fieldOrFun) == "function") {
			continue;
		}
		var value = obj[i];
		eval("result." + i + "='" + value + "'");
	}
	return result;
}